package com.sly.demoim.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring容器bean工具类型
 *
 * @author SLY
 * @date 2022/7/17
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext act;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        act = applicationContext;
    }

    /**
     * 根据bean的名字获取工厂中对应的bean对象
     *
     * @param beanName bean名称
     * @return bean对象
     */
    public Object getBean(String beanName) {
        return act.getBean(beanName);
    }

    /**
     * 根据bean的名字获取工厂中对应的bean对象
     *
     * @param beanName      bean名称
     * @param requiredClass bean类型
     * @return bean对象
     */
    public <T> T getBean(String beanName, Class<T> requiredClass) {
        return act.getBean(beanName, requiredClass);
    }


}
